/**
 * 
 */
package com.yangkun.test;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Company: </p> 
 * @author Administrator
 * @date 2017年7月28日
 */
import java.util.Map;  
import java.util.Set;  
import java.util.concurrent.ConcurrentHashMap;  
  
import org.springframework.web.socket.TextMessage;  
import org.springframework.web.socket.WebSocketSession;  
  
public class WebSocketSessionUtil {  
  
    private static Map<String, WebSocketSession> clients = new ConcurrentHashMap<>();  
  
    public static void add(String userName, WebSocketSession session) {  
        clients.put(userName, session);  
    }  
  
    public static WebSocketSession get(String userName) {  
        return clients.get(userName);  
    }  
  
    public static void remove(String userName) {  
        clients.remove(userName);  
    }  
  
    public static void broadcast(TextMessage message)  
            throws Exception {  
  
        Set<String> allUsers = clients.keySet();  
  
        for (String name : allUsers) {  
            WebSocketSession session = clients.get(name);  
            if (session.isOpen()){
            	try {
            		session.sendMessage(message);  
				} catch (Exception e) {
					e.printStackTrace();
				}
            }
        }  
    }  
}  
